##  GUI应用程序主程序入口

import sys

from PyQt5.QtWidgets import QApplication

from myMainWindow import QmyMainWindow

from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QLabel, QComboBox, QLineEdit, QPushButton, QMessageBox, \
	QDesktopWidget,QHBoxLayout,QRadioButton

from PyQt5.QtGui import QIcon, QPalette, QColor


class Login(QWidget):  # 登录页面
	def __init__(self):
		super().__init__()
		self.main_window = None
		self.is_eng = True
		self.setWindowIcon(QIcon('resources/mainico.png'))
		# 设置背景颜色
		palette = QPalette()
		palette.setColor(QPalette.Background, QColor(173, 216, 230))
		self.setPalette(palette)
		self.initUI()
	
	def initUI(self):
		para = 'User Login' if self.is_eng else '用户登录'
		self.setWindowTitle(f'用户登录(User Login)')
		self.setGeometry(300, 300, 400, 250)
		# self.setFixedSize(self.width(), self.height())  # 固定窗口大小
		self.setStyleSheet("""
            QWidget {
                background-color: #f0f0f0;
            }
            QLabel {
                font-size: 16px;
                color: #333;
                font-weight: bold;  /* 设置标签字体加粗 */
            }
            QComboBox {
                height: 30px;
                border: 1px solid #ccc;
                border-radius: 3px;
                padding-left: 5px;
                font-size: 16px;
                font-weight: bold;  /* 设置标签字体加粗 */
            }
            QLineEdit {
                height: 30px;
                border: 1px solid #ccc;
                border-radius: 3px;
                padding-left: 5px;
                background-color: white;
                font-size: 16px;
            }
            QPushButton {
                height: 35px;
                background-color: #007BFF;
                color: white;
                border: none;
                border-radius: 3px;
                font-size: 16px;
                font-weight: bold;  /* 设置按钮字体加粗 */
            }
            QPushButton:hover {
                background-color: #0056b3;
            }
        """)
		
		layout = QVBoxLayout()
		layout.setSpacing(15)
		layout.setContentsMargins(50, 20, 50, 20)
		
		# 用户类型标签和下拉框
		para = 'user type:' if self.is_eng else '用户类型:'
		user_type_label = QLabel(f'用户类型(User Type)')
		self.user_type_combo = QComboBox()
		self.user_type_combo.addItems(['Admin', 'User', 'Eng', 'QC', 'Sales'])
		layout.addWidget(user_type_label)
		layout.addWidget(self.user_type_combo)
		
		# 密码标签和输入框
		para = 'password:' if self.is_eng else '密码:'
		pwd_label = QLabel(f'密码(Password)')
		self.pwd_lineedit = QLineEdit()
		self.pwd_lineedit.setEchoMode(QLineEdit.Password)
		self.pwd_lineedit.returnPressed.connect(self.handle_login)
		layout.addWidget(pwd_label)
		layout.addWidget(self.pwd_lineedit)
		
		# 登录按钮和单选框布局
		button_layout = QHBoxLayout()
		
		# 单选框
		radio_layout = QHBoxLayout()
		self.chinese_radio = QRadioButton('Chinese')
		self.english_radio = QRadioButton('English')
		radio_layout.addWidget(self.chinese_radio)
		radio_layout.addWidget(self.english_radio)
		self.chinese_radio.setChecked(True)
		button_layout.addLayout(radio_layout)
		# 登录按钮
		para = 'Login' if self.is_eng else '登录'
		login_button = QPushButton(f'登录(Login)')
		login_button.clicked.connect(self.handle_login)
		button_layout.addWidget(login_button)
		
		layout.addLayout(button_layout)
	
		self.setLayout(layout)
		self.center()
	
	def center(self):
		qr = self.frameGeometry()
		cp = QDesktopWidget().availableGeometry().center()
		qr.moveCenter(cp)
		self.move(qr.topLeft())
	
	def handle_login(self):
		self.is_eng = False if self.chinese_radio.isChecked() else True
		self.main_window = QmyMainWindow(is_eng=self.is_eng)
		user_type = self.user_type_combo.currentText()
		password = self.pwd_lineedit.text()
		correct_pwd = self.main_window.get_pwd(user_type)  # 正确密码
		print(f"数据库中{user_type}的密码是{correct_pwd}")
		# 如果密码正确
		if password == correct_pwd:
			self.close()
			# self.main_window.is_eng = self.is_eng
			if user_type == "Admin":
				self.main_window.admin_mode()
			elif user_type == "User":
				self.main_window.user_mode()
			elif user_type == "Eng":
				self.main_window.eng_mode()
			elif user_type == "QC":
				self.main_window.QC_mode()
			elif user_type == 'Sales':
				self.main_window.sales_mode()
			self.main_window.show()
		else:
			QMessageBox.warning(self, '登录失败', '用户名或密码错误，请重试！')


if __name__ == "__main__":
	app = QApplication(sys.argv)  # 创建GUI应用程序
	# mainform = QmyMainWindow()  # 创建主窗体
	Login = Login()  # 创建登录界面
	Login.show()  # 显示主窗体
	sys.exit(app.exec_())
